<template>
    <!-- 忘记密码 -->
    <el-dialog v-model="state.forgetPasswordDialog" title="忘记密码" width="400px">
        <el-form class="login-form" :label-position="'top'" :rules="rules">
            <el-form-item label="输入您的注册账号:" prop="account">
                <el-input v-model="forgetData.account" placeholder="请输入您的注册账号"></el-input>
            </el-form-item>
            <el-form-item label="输入您的个人邮箱:" prop="email">
                <el-input v-model="forgetData.email" placeholder="请输入您的个人邮箱"></el-input>
            </el-form-item>
        </el-form>
        <!-- 底部内容 -->
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="state.forgetPasswordDialog = false">取消</el-button>
                <el-button type="primary" @click="verifyAccount">下一步</el-button>
            </div>
        </template>
    </el-dialog>
    <!-- 修改密码 -->
    <el-dialog v-model="state.changePasswordDialog" title="修改密码" width="400px">
        <el-form class="login-form" :label-position="'top'" :rules="rules">
            <el-form-item label="输入新密码:" prop="password">
                <el-input v-model="forgetData.password" placeholder="请输入新密码,6-12位密码包含大小写字母数字" show-password></el-input>
            </el-form-item>
            <el-form-item label="再次输入新密码:" prop="repassword">
                <el-input v-model="forgetData.repassword" placeholder="请再次输入新密码" show-password></el-input>
            </el-form-item>
        </el-form>
        <!-- 底部内容 -->
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="state.changePasswordDialog = false">取消</el-button>
                <el-button type="primary" @click="resetPassword">确定</el-button>
            </div>
        </template>
    </el-dialog>
</template>
<script lang="ts" setup>
    import { reactive, ref, defineExpose } from 'vue'
    import { resetPassword as reset, verify } from '@/api/login.js'
    import { ElMessage } from 'element-plus'
    //表单对象接口
    interface forgetData {
        account: string;
        email: string;
        password: string;
        repassword: string;
    }
    //表单对象
    const forgetData = reactive({
        account: '',
        email: '',
        password: '',
        repassword: ''
    })
    // 控制弹窗默认关闭
    const state = reactive({
        forgetPasswordDialog: false,
        changePasswordDialog: false
    })
    // 打开弹窗
    const open = () => {
        state.forgetPasswordDialog = true
    }
    // ... existing code ...
    /**
     * 验证账号与邮箱是否匹配
     * 1. 调用 verify 接口验证用户输入的账号和邮箱是否匹配
     * 2. 如果验证成功，将用户 ID 存储到 sessionStorage，并切换到修改密码弹窗
     * 3. 如果验证失败，显示错误提示信息
     */
    const verifyAccount = async () => {
        const res = await verify(forgetData)
        if (res.status === 0) {
            ElMessage({
                message: '验证成功！',
                type: 'success',
            })
            sessionStorage.setItem('id', res.id)
            state.forgetPasswordDialog = false
            state.changePasswordDialog = true
        } else {
            ElMessage.error('验证失败！')
        }
    }

    /**
     * 重置密码逻辑
     * 1. 检查两次输入的新密码是否一致
     * 2. 如果一致，从 sessionStorage 获取用户 ID 并构造请求数据
     * 3. 调用 reset 接口提交新密码
     * 4. 显示修改成功的提示信息
     * 5. 如果密码不一致，显示错误提示信息
     */
    const resetPassword = async () => {
        if (forgetData.repassword === forgetData.password) {
            const newPassword = forgetData.repassword
            const data = { id: sessionStorage.getItem('id'), newPassword }
            await reset(data)
            ElMessage({
                message: '修改成功！',
                type: 'success',
            })
            changePasswordDialog: false
        } else {
            ElMessage.error('修改失败，请检查密码是否一致！')
        }
    }

    const rules = reactive({
        account: [
            { required: true, message: '请输入您的注册账号', trigger: 'blur' }
        ],
        email: [
            { required: true, message: '请输入您的注册邮箱', trigger: 'blur' }
        ],
        password: [
            { required: true, message: '请输入您的新密码', trigger: 'blur' }
        ],
        repassword: [
            { required: true, message: '请再次输入您的新密码', trigger: 'blur' }
        ]
    })
    defineExpose({
        open
    })
</script>
<style scoped lang="scss"></style>